package cn.goktech.controller;

import cn.goktech.bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class ParamsController {

    /*
     * 方式一：通过request作用域获取数据，再方法的参数中定义HttpServletRequest
     * */
    @RequestMapping("/params01")
    public String getParams(HttpServletRequest request) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        System.out.println("id=" + id);
        System.out.println("name=" + name);
        return "success";
    }
    /*
     * 方式二：将请求参数作为方法的参数，Springmvc会自动封装
     * */
    @RequestMapping("/params02")
    public String getParams2(String id,String name) {
        System.out.println("id=" + id);
        System.out.println("name=" + name);
        return "success";
    }
    @RequestMapping("/params03")
    public String getParams3(Integer id,String name) {
        System.out.println("id=" + id);
        System.out.println("name=" + name);
        return "success";
    }
    /*
     * 方式三：将请求参数作封装为bean对象
     * */
    @RequestMapping("/params04")
    public String getParams4(User user) {
        System.out.println("user="+user);

        return "success";
    }
    /*
     * 方式四：当请求参数与方法参数名不一致，需要做处理
     * @RequestParam("请求参数名") 参数：将请求参数映射到方法参数来接收
     * */
    @RequestMapping("/params05")
    public String getParams5(@RequestParam("ids") String id, @RequestParam("names") String name) {
        System.out.println("id=" + id);
        System.out.println("name=" + name);

        return "success";
    }
}
